<template>
  <div>
    <el-dialog title="新增部门" :visible="showDialog" @close="btnCancel">
      <el-form label-width="120px" :model="formData">
        <el-form-item label="部门名称">
          <el-input v-model="formData.name" style="width: 80%" />
        </el-form-item>
        <el-form-item label="部门编码">
          <el-input v-model="formData.code" style="width: 80%" />
        </el-form-item>
        <el-form-item label="部门负责人">
          <el-select v-model="formData.manager" placeholder="请选择" style="width: 80%" @focus="getEmployeeSimple">
            <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍">
          <el-input v-model="formData.introduce" style="width: 80%" />
        </el-form-item>
      </el-form>

      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments } from '@/api/departments'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      peoples: []
    }
  },
  computed: {},

  mounted() {},

  methods: {
    async getEmployeeSimple() {
      this.peoples = await getEmployeeSimple()
    },
    async btnOK() {
      await addDepartments({ ...this.formData, pid: this.treeNode.id })
      this.$emit('addDepts')
      // this.$emit('changedialog', false) // 触发事件
      this.$emit('update:showDialog', false)
    },
    btnCancel() {
      // this.$refs.deptForm.resetFields() // 重置校验字段
      this.$emit('update:showDialog', false) // 关闭
    }
  }
}
</script>
<style lang="scss" scoped></style>
